package com.example.miaosha.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

/**
 * @ClassName: WebServerConfiguration
 * @Signature: Created by lerry_li on 2020/11/10
 * @Description: 当Spring容器内没有TomcatEmbeddedServletContainerFactory这个bean时，会把此bean加载进Spring容器
 */
@Component
public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        //使用对应工厂类提供的接口，定制化tomcat connector
        ((TomcatServletWebServerFactory) factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

                //定制化
                // 设置30内没有请求，则服务端自动断开keep alive
                protocol.setKeepAliveTimeout(30000);
                // 当客户端发送超过3000次请求时，则服务端自动断开keep alive
                protocol.setMaxKeepAliveRequests(3000);
            }
        });
    }
}
